"""

@FileName：Daily Trading Information
@Author：马时易初
@Time：2024/4/5 14:34\n
@Copyright：©2019-2024 研发中心

"""
import scrapy
from lxml import etree
from 爬虫.eastmoney.eastmoney.items import company_transaction_data_daily

class company_transaction_data_daily_spider1(scrapy.Spider):  # 继承scrapy.Spider类
    name = 'company_transaction_data_daily'  # spider名称，为spider唯一标识
    allowed_domains = ['data.eastmoney.com']  # 允许此爬虫抓取的域的字符串的可选列表，指定一个列表可以抓取，其它就不会抓取了
    start_urls = ["https://quote.eastmoney.com/unify/r/1.688981"]  # 爬取起始页，当没有指定特定网址时，爬虫将开始抓取的网址列表
    #指定pipelines，如果不指定，则在settings中申明了多少，就用多少
    custom_settings = {
        'ITEM_PIPELINES': {'爬虫.eastmoney.eastmoney.pipelines.ConsolePipeline': 300},
        'ITEM_PIPELINES': {'爬虫.eastmoney.eastmoney.pipelines.MysqlPipeline': 200},
    }
    def parse(self,response):
        item = company_transaction_data_daily()     # 定义Item类的对象，用于保存一条数据
        html = etree.HTML(response.text)
        item['today_open'] =html.xpath('//div[@class="brief_info_c"]/table/tbody/tr[1]/td[6]/span')[0].text  # 今开
        item['highest'] = html.xpath('//div[@class="brief_info_c"]/table/tbody/tr[1]/td[4]/span')[0].text  # 最高

        item['turnover'] =html.xpath('//div[@class="brief_info_c"]/table/tbody/tr[1]/td[8]')[0].text  # 换手
        item['trading_volume'] =html.xpath('//div[@class="brief_info_c"]/table/tbody/tr[2]/td[2]')[0].text  # 成交量
        # item['market_value_added'] = wd.find_elements(By.XPATH, '//div[@class="brief_info_c]/table/tbody/tr/td')  # 市盈（动）
        item['market_value_added'] = None
        item['total_market_value'] = html.xpath('//div[@class="brief_info_c"]/table/tbody/tr[1]/td[10]')[0].text  # 总市值
        item['yesterday_close'] =html.xpath('//div[@class="brief_info_c"]/table/tbody/tr[2]/td[6]')[0].text  # 昨收
        item['lowest'] =html.xpath('//div[@class="brief_info_c"]/table/tbody/tr[2]/td[4]')[0].text  # 最低
        item['volume_ratio'] =html.xpath('//div[@class="brief_info_c"]/table/tbody/tr[2]/td[8]')[0].text  # 量比
        item['trading_amount'] =html.xpath('//div[@class="brief_info_c"]/table/tbody/tr[3]/td[2]')[0].text  # 成交额
        item['market_value_clean'] = html.xpath('//div[@class="brief_info_c"]/table/tbody/tr[3]/td[10]')[0].text  # 市净
        item['circulation_market_value'] = html.xpath('//div[@class="brief_info_c"]/table/tbody/tr')
        yield item








        # 获取表格数据
        # rows = html.xpath('//div[@class="brief_info_c"]/table/tbody/tr')
        # for row in rows:
        #         cols = row.xpath('.//td')
        #         item['stock_code'] = cols[1].xpath('.//a/text()')
        #         item['stock_name'] = cols[2].xpath('.//a/span/text()')
        #         # item["close_price"] = cols[3].xpath('.//span/text()')
        #         # item["main_industry"] = cols[5].xpath('.//span/text()')
        #         # item["region"] = cols[6].xpath('.//span/text()')
        #         # item["weight"] = 0
        #         # item["earnings_per_share"] = cols[7].xpath('./text()')
        #         # item["net_asset_per_share"] = cols[8].xpath('./text()')
        #         # item["net_assets"] = 0
        #         # item["return_rate"] = cols[9].xpath('./text()')
        #         # item["total_shares"] = cols[10].xpath('./text()')
        #         # item["circulation_shares"] = cols[11].xpath('./text()')
        #         # item["circulation_market_value"] = cols[12].xpath('./text()')
        #         # item["related_info"] =""
        #         print(item['stock_code'], item['stock_name'])
        #         yield item
# class company_transaction_data_daily(scrapy.Item):
#     today_open= scrapy.Field() # 今开
#     highest= scrapy.Field() # 最高
#     turnover= scrapy.Field() # 换手
#     trading_volume= scrapy.Field() # 成交量
#     market_value_added= scrapy.Field() # 市盈(动)
#     total_market_value= scrapy.Field() # 总市值
#     yesterday_close= scrapy.Field() # 昨收
#     lowest= scrapy.Field() # 最低
#     volume_ratio= scrapy.Field() # 量比
#     trading_amount= scrapy.Field() # 成交额
#     market_value_clean= scrapy.Field() # 市净
#     circulation_market_value= scrapy.Field() # 流通市值
